package br.com.massai.model.business;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import br.com.massai.controller.adapter.FunctionAdapter;
import br.com.massai.model.dao.FunctionRepository;
import br.com.massai.model.entity.Function;
import br.com.massai.model.exceptions.SystemException;
import br.com.massai.view.enums.Functionality;

@Stateless
public class FunctionFacade implements FunctionBusiness {

	private static final long serialVersionUID = -1337058363594059807L;

	@EJB private FunctionRepository functionRepository;

	@Override
	public Function findID(Long id) throws SystemException {
		return functionRepository.findID(id);
	}

	@Override
	public Function save(Function entity) throws SystemException {
		return null;
	}

	@Override
	public void delete(Function entity) throws SystemException {
		return;
	}

	@Override
	public void checkBusinessRule(Function entity) throws SystemException {
		return;
	}

	@Override
	public Function getFunctionByAcronym(String acronym) throws SystemException {
		return functionRepository.getFunctionByAcronym(acronym);
	}

	@Override
	public List<Function> findAll() throws SystemException {
		return functionRepository.findAll();
	}

	@Override
	public List<FunctionAdapter> getFunctionsAdapter() throws SystemException {
		List<FunctionAdapter> adapters = new ArrayList<>();
		try{
			List<Function> functions = functionRepository.findAllOrdered();
			for(Function f : functions)
				adapters.add(new FunctionAdapter(f));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return adapters;
	}

	@Override
	public List<FunctionAdapter> getFunctionsAdapter(Functionality functionality) throws SystemException {
		List<FunctionAdapter> adapters = new ArrayList<>();
		try{
			List<Function> functions = functionRepository.findOrderedByFunctionality(functionality);
			for(Function f : functions)
				adapters.add(new FunctionAdapter(f));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return adapters;
	}
}